<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    x: {{ x }} <br />
    y: {{ y }}
    <button @click="increase">+</button>
    {{ count }}
  </div>
  <script type="module">
    import { createApp, reactive, toRefs, onMounted, onUnmounted, ref } from '../node_modules/vue/dist/vue.esm-browser.js';
    // vue2.x 的option API
    /**const app = createApp({
      data() {
        return {
          position: {
            x: 0,
            y: 0
          }
        }
      },
      mounted() {
        this.position.x = 300;
      }
    });*/
    const useMousemove = () => {
      const position = reactive({
        x: 0,
        y: 0
      });
      const update = e => {
        position.x = e.pageX;
        position.y = e.pageY;
      }

      onMounted(() => {
        window.addEventListener('mousemove', update);
      })

      onUnmounted(() => {
        window.removeEventListener('mousemove', update);
      })
      return toRefs(position);
    }

    const useCount = () => {
      // 数值0转化为响应式对象，count.value = 0
      const count = ref(0);
      return {
        count,
        increase() {
          count.value++;
        }
      }
    }

    const app = createApp({
      setup() {
        const { x, y } = useMousemove();
        return { x, y, ...useCount() }
      }
    })

    app.mount('#app');
  </script>
</body>
</html>